package com.stylefeng.guns.rest.common.persistence.mapper;

import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.api.order.vo.OrderVo;
import com.stylefeng.guns.rest.common.persistence.model.MoocOrderT;
import com.baomidou.mybatisplus.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * <p>
 * 订单信息表 Mapper 接口
 * </p>
 *
 * @author zhaoluowei
 * @since 2019-09-18
 */
@Mapper
@Repository(value = "moocOrderTMapper")
public interface MoocOrderTMapper extends BaseMapper<MoocOrderT> {

    @Select("SELECT h.seat_address AS seatAddress \n" +
            "FROM mooc_field_t f,mooc_hall_dict_t h\n" +
            "WHERE f.hall_id = h.UUID AND\n" +
            "      f.UUID = #{fieldId} ")
    String getSeatsByFieldId(@Param("fieldId") String fieldId);


    @Select("SELECT \n" +
            "   o.UUID AS orderId,\n" +
            "\t h.film_name AS filmName,\n" +
            "   CONCAT('今天 ', DATE_FORMAT(o.order_time,'%m月%d日 ') ,f.begin_time)   AS fieldTime,\n" +
            "   c.cinema_name AS cinemaName,\n" +
            "\t o.seats_name AS seatsName,\n" +
            "   o.order_price AS orderPrice,\n" +
            "   o.order_status AS orderStatus,\n" +
            "\t UNIX_TIMESTAMP(o.order_time)  AS orderTimestamp\n" +
            "\n" +
            "FROM mooc_order_t o,mooc_field_t f ,mooc_hall_film_info_t h,mooc_cinema_t c\n" +
            "WHERE o.cinema_id = c.UUID AND\n" +
            "      o.field_id = f.UUID AND\n" +
            "\t\t\to.film_id = h.film_id AND\n" +
            "\t\t\to.UUID = #{orderId} ")
    OrderVo getOrderIdById(@Param("orderId") String orderId);

    @Select("SELECT \n" +
            "   o.UUID AS orderId,\n" +
            "\t h.film_name AS filmName,\n" +
            "   CONCAT(DATE_FORMAT(o.order_time,'%y年%m月%d日 ') ,f.begin_time)   AS fieldTime,\n" +
            "   c.cinema_name AS cinemaName,\n" +
            "\t o.seats_name AS seatsName,\n" +
            "   o.order_price AS orderPrice,\n" +
            "   o.order_status AS orderStatus,\n" +
            "\t UNIX_TIMESTAMP(o.order_time)  AS orderTimestamp\n" +
            "\n" +
            "FROM mooc_order_t o,mooc_field_t f ,mooc_hall_film_info_t h,mooc_cinema_t c\n" +
            "WHERE o.cinema_id = c.UUID AND\n" +
            "      o.field_id = f.UUID AND\n" +
            "\t\t\to.film_id = h.film_id AND\n" +
            "\t\t\to.order_user = #{userId} ")
    List<OrderVo> getOrderInfoById(@Param("userId") Integer userId,
                                   @Param("page") Page<OrderVo> page);


    @Select("SELECT GROUP_CONCAT(seats_ids)\n" +
            "FROM  mooc_order_t\n" +
            "WHERE field_id = #{fieldId} ")
    String getSoldSeatsByFieldId(@Param("fieldId") Integer fieldId);


    @Update("UPDATE mooc_order_t SET order_status = 1 WHERE UUID = #{moocOrderT} ")
    Integer updateByStringId(@Param("moocOrderT") String moocOrderT);

    @Update("UPDATE mooc_order_t SET order_status = 2 WHERE UUID = #{moocOrderT} ")
    Integer updateByStringPayFailId(@Param("moocOrderT") String moocOrderT);

}
